package com.cdm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.cdm.pojo.Activity;
import com.cdm.service.ActivityService;

@Controller
@RequestMapping("/activity")
public class ActivityController {

	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private HttpServletRequest request;
	
	private String[] labels = {"推荐","运动","技能","美食","旅行","迷幻","讲座","其它"};
	
	@RequestMapping("/getRecommended")
	public void getRecommended(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer pageNow  = Integer.parseInt(request.getParameter("pageNow"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		map.put("index", (pageNow -1)*pageSize);
 		map.put("pageSize",pageSize);
 		map.put("isClassify", 1);
		List<Activity> activities = activityService.findAllClassify(map);
		Integer infoSum = activityService.findActivityCount(map);
		JSONObject jsonObject=new JSONObject();
		if (activities!=null && activities.size()>0) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("pageNow", pageNow);
		jsonObject.put("pageSize", pageSize);
		jsonObject.put("infoSum", infoSum);
		jsonObject.put("pageSum", infoSum % pageSize == 0 ? infoSum/pageSize :infoSum/pageSize + 1);
		jsonObject.put("activityList", activities);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/getByLable")
	public void getByLable(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer pageNow  = Integer.parseInt(request.getParameter("pageNow"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int lablePosition = Integer.parseInt(request.getParameter("lablePosition"));
		String activityLabel = labels[lablePosition];
		map.put("index", (pageNow -1)*pageSize);
 		map.put("pageSize",pageSize);
 		if (lablePosition == 0) {
 	 		map.put("isClassify", 1);
 	 		//System.out.println("sas");
		}else {
	 		map.put("activityLabel", activityLabel);
 	 		//System.out.println("sas"+activityLabel);
		}
		List<Activity> activities = activityService.findAllClassify(map);
		Integer infoSum = activityService.findActivityCount(map);
		JSONObject jsonObject=new JSONObject();
		if (activities!=null && activities.size()>0) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("pageNow", pageNow);
		jsonObject.put("pageSize", pageSize);
		jsonObject.put("infoSum", infoSum);
		jsonObject.put("pageSum", infoSum % pageSize == 0 ? infoSum/pageSize :infoSum/pageSize + 1);
		jsonObject.put("activityList", activities);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/getByTheme")
	public void getByTheme(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer pageNow  = Integer.parseInt(request.getParameter("pageNow"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String activityTheme = request.getParameter("activityTheme");
		map.put("index", (pageNow -1)*pageSize);
 		map.put("pageSize",pageSize);
 		map.put("activityTheme", activityTheme);
		List<Activity> activities = activityService.findAllClassify(map);
		Integer infoSum = activityService.findActivityCount(map);
		JSONObject jsonObject=new JSONObject();
		if (activities!=null && activities.size()>0) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("pageNow", pageNow);
		jsonObject.put("pageSize", pageSize);
		jsonObject.put("infoSum", infoSum);
		jsonObject.put("pageSum", infoSum % pageSize == 0 ? infoSum/pageSize :infoSum/pageSize + 1);
		jsonObject.put("activityList", activities);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/getByPublisher")
	public void getByPublisher(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer pageNow  = Integer.parseInt(request.getParameter("pageNow"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String userId = request.getParameter("userId");
		String activityCity = request.getParameter("activityCity");
		String activityLabel = request.getParameter("activityLabel");
		String stateId = request.getParameter("stateId");
		map.put("index", (pageNow -1)*pageSize);
 		map.put("pageSize",pageSize);
 		map.put("userId", userId);
		map.put("activityCity", activityCity);
 		map.put("activityLabel",activityLabel);
 		map.put("stateId", stateId);
		List<Activity> activities = activityService.getByPublisher(map);
		Integer infoSum = activityService.findActivityCountByPublisher(map);
		JSONObject jsonObject=new JSONObject();
		if (activities!=null && activities.size()>0) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("pageNow", pageNow);
		jsonObject.put("pageSize", pageSize);
		jsonObject.put("infoSum", infoSum);
		jsonObject.put("pageSum", infoSum % pageSize == 0 ? infoSum/pageSize :infoSum/pageSize + 1);
		jsonObject.put("activityList", activities);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/getByJoiner")
	public void getByJoiner(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer pageNow  = Integer.parseInt(request.getParameter("pageNow"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String userId = request.getParameter("userId");
		String activityCity = request.getParameter("activityCity");
		String activityLabel = request.getParameter("activityLabel");
		String stateId = request.getParameter("stateId");
		map.put("index", (pageNow -1)*pageSize);
 		map.put("pageSize",pageSize);
 		map.put("userId", userId);
		map.put("activityCity", activityCity);
 		map.put("activityLabel",activityLabel);
 		map.put("stateId", stateId);
		List<Activity> activities = activityService.getByJoiner(map);
		Integer infoSum = activityService.findActivityCountByJoiner(map);
		JSONObject jsonObject=new JSONObject();
		if (activities!=null && activities.size()>0) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("pageNow", pageNow);
		jsonObject.put("pageSize", pageSize);
		jsonObject.put("infoSum", infoSum);
		jsonObject.put("pageSum", infoSum % pageSize == 0 ? infoSum/pageSize :infoSum/pageSize + 1);
		jsonObject.put("activityList", activities);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/addActivity")
	public void addActivity(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		String activityLabel = request.getParameter("activityLabel");
		String activityTheme = request.getParameter("activityTheme");
		Long activityBegintime = Long.parseLong(request.getParameter("activityBegintime"));
		Long activityEndtime = Long.parseLong(request.getParameter("activityEndtime"));
		Long activityCreattime = Long.parseLong(request.getParameter("activityCreattime"));
		String activityAddress = request.getParameter("activityAddress");
		String activityImgurl = request.getParameter("activityImgurl");
		String activityDesc = request.getParameter("activityDesc");
		String activityCare = request.getParameter("activityCare");
		String activityMaxPeopleNumber = request.getParameter("activityMaxPeopleNumber");
		String isClassify = request.getParameter("isClassify");
		String userId = request.getParameter("userId");
		String stateId = request.getParameter("stateId");
		String activityProvince = request.getParameter("activityProvince");
		String activityCity = request.getParameter("activityCity");
		map.put("activityLabel", activityLabel);
 		map.put("activityTheme",activityTheme);
 		map.put("activityBegintime", longToDatestring(activityBegintime));
		map.put("activityEndtime", longToDatestring(activityEndtime));
		map.put("activityCreattime", longToDatestring(activityCreattime));
 		map.put("activityAddress",activityAddress);
 		map.put("activityImgurl", activityImgurl);
		map.put("activityDesc", activityDesc);
 		map.put("activityCare",activityCare);
 		map.put("stateId", stateId);
		map.put("activityMaxPeopleNumber", activityMaxPeopleNumber);
 		map.put("isClassify",isClassify);
 		map.put("userId", userId);
 		map.put("activityProvince",activityProvince);
 		map.put("activityCity", activityCity);
 		System.out.println("ssss="+map.toString());
		Boolean sucess = activityService.addActivity(map);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result", sucess);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/deleteActivity")
	public void deleteActivity(HttpServletResponse response) throws Exception{
		Integer activityId  = Integer.parseInt(request.getParameter("activityId"));
		Boolean sucess = activityService.deleteActivity(activityId);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result", sucess);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/deleteJoiner")
	public void deleteJoiner(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer activityId  = Integer.parseInt(request.getParameter("activityId"));
		Integer userId  = Integer.parseInt(request.getParameter("userId"));
		//System.out.println("=="+activityId+userId);
		map.put("activityId", activityId);
		map.put("userId", userId);
		Boolean sucess = activityService.deleteJoiner(map);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result", sucess);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	@RequestMapping("/addJoiner")
	public void addJoiner(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer activityId  = Integer.parseInt(request.getParameter("activityId"));
		Integer userId  = Integer.parseInt(request.getParameter("userId"));
		//System.out.println("=="+activityId+userId);
		map.put("activityId", activityId);
		map.put("userId", userId);
		Boolean sucess = activityService.addJoiner(map);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result", sucess);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
	
	private String longToDatestring(long time){
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
	
}
